package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.outside.hotel.query.*;
import com.ctshk.rpc.booking.dto.outside.hotel.query.*;
import com.ctshk.rpc.booking.entity.*;
import com.ctshk.rpc.booking.mapper.*;
import com.ctshk.rpc.booking.req.outside.hotel.save.*;
import com.ctshk.rpc.booking.req.outside.hotel.save.*;
import com.ctshk.rpc.booking.service.ISupplierOutsideOrderHotelCheckInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 站外酒店入住信息 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-09
 */
@DubboService
public class SupplierOutsideOrderHotelCheckInServiceImpl implements ISupplierOutsideOrderHotelCheckInService {
    @Autowired
    SupplierOutsideOrderHotelCheckInMapper supplierOutsideOrderHotelCheckInMapper;
    @Autowired
    SupplierOutsideOrderHotelRoomMapper supplierOutsideOrderHotelRoomMapper;
    @Autowired
    SupplierOutsideOrderHotelTouristMapper supplierOutsideOrderHotelTouristMapper;
    @Autowired
    SupplierOutsideOrderHotelServiceMapper supplierOutsideOrderHotelServiceMapper;
    @Autowired
    SupplierOutsideOrderHotelServiceTouristTypeMapper supplierOutsideOrderHotelServiceTouristTypeMapper;
    @Autowired
    SupplierOutsideOrderHotelServiceTouristMapper supplierOutsideOrderHotelServiceTouristMapper;

    @Override
    public void save(Long supplierOrderId, Long userId, LocalDateTime nowDateTime, List<SupplierOutsideOrderHotelCheckInReq> supplierOutsideOrderHotelReqList) {
        if (supplierOrderId != null) {
            //删除
            QueryWrapper<SupplierOutsideOrderHotelCheckIn> outsideOrderHotelCheckInWrapper = new QueryWrapper<>();
            outsideOrderHotelCheckInWrapper.lambda().eq(SupplierOutsideOrderHotelCheckIn::getOrderId, supplierOrderId);
            supplierOutsideOrderHotelCheckInMapper.delete(outsideOrderHotelCheckInWrapper);
            QueryWrapper<SupplierOutsideOrderHotelRoom> outsideOrderHotelRoomWrapper = new QueryWrapper<>();
            outsideOrderHotelRoomWrapper.lambda().eq(SupplierOutsideOrderHotelRoom::getOrderId, supplierOrderId);
            supplierOutsideOrderHotelRoomMapper.delete(outsideOrderHotelRoomWrapper);
            QueryWrapper<SupplierOutsideOrderHotelTourist> outsideOrderHotelTouristWrapper = new QueryWrapper<>();
            outsideOrderHotelTouristWrapper.lambda().eq(SupplierOutsideOrderHotelTourist::getOrderId, supplierOrderId);
            supplierOutsideOrderHotelTouristMapper.delete(outsideOrderHotelTouristWrapper);
            // service
            QueryWrapper<SupplierOutsideOrderHotelService> outsideOrderHotelTouristServiceWrapper = new QueryWrapper<>();
            outsideOrderHotelTouristServiceWrapper.lambda().eq(SupplierOutsideOrderHotelService::getOrderId, supplierOrderId);
            supplierOutsideOrderHotelServiceMapper.delete(outsideOrderHotelTouristServiceWrapper);
            QueryWrapper<SupplierOutsideOrderHotelServiceTouristType> outsideOrderHotelServiceTouristTypeWrapper = new QueryWrapper<>();
            outsideOrderHotelServiceTouristTypeWrapper.lambda().eq(SupplierOutsideOrderHotelServiceTouristType::getOrderId, supplierOrderId);
            supplierOutsideOrderHotelServiceTouristTypeMapper.delete(outsideOrderHotelServiceTouristTypeWrapper);
            QueryWrapper<SupplierOutsideOrderHotelServiceTourist> outsideOrderHotelServiceTouristWrapper = new QueryWrapper<>();
            outsideOrderHotelServiceTouristWrapper.lambda().eq(SupplierOutsideOrderHotelServiceTourist::getOrderId, supplierOrderId);
            supplierOutsideOrderHotelServiceTouristMapper.delete(outsideOrderHotelServiceTouristWrapper);
        }
        List<SupplierOutsideOrderHotelCheckIn> supplierOutsideOrderHotelCheckIns = new ArrayList<>();
        List<SupplierOutsideOrderHotelRoom> supplierOutsideOrderHotelRooms = new ArrayList<>();
        List<SupplierOutsideOrderHotelTourist> supplierOutsideOrderHotelTourists = new ArrayList<>();
        List<SupplierOutsideOrderHotelService> supplierOutsideOrderHotelServices = new ArrayList<>();
        List<SupplierOutsideOrderHotelServiceTouristType> supplierOutsideOrderHotelServiceTouristTypes = new ArrayList<>();
        List<SupplierOutsideOrderHotelServiceTourist> supplierOutsideOrderHotelServiceTourists = new ArrayList<>();

        //新增餐饮信息
        supplierOutsideOrderHotelReqList.forEach(outsideOrderHotelReq -> {
            SupplierOutsideOrderHotelCheckIn supplierOutsideOrderHotel = EntityUtil.copy(outsideOrderHotelReq, SupplierOutsideOrderHotelCheckIn.class);
            supplierOutsideOrderHotel.setId(SnowflakeIdWorker.nextId());
            supplierOutsideOrderHotel.setOrderId(supplierOrderId);
            supplierOutsideOrderHotel.setCreateId(userId);
            supplierOutsideOrderHotel.setGmtCreate(nowDateTime);
            supplierOutsideOrderHotel.setModifiedId(userId);
            supplierOutsideOrderHotel.setGmtModified(nowDateTime);
            supplierOutsideOrderHotel.setIsDeleted(IsDeletedCode.NO.getCode());
            supplierOutsideOrderHotelCheckIns.add(supplierOutsideOrderHotel);

            if (CollectionUtils.isNotEmpty(outsideOrderHotelReq.getSupplierOutsideOrderHotelRoomReqList())) {
                //新增酒店房间详细信息
                outsideOrderHotelReq.getSupplierOutsideOrderHotelRoomReqList().forEach(outsideOrderHotelRoomReq -> {
                    SupplierOutsideOrderHotelRoom supplierOutsideOrderHotelRoom = EntityUtil.copy(outsideOrderHotelRoomReq, SupplierOutsideOrderHotelRoom.class);
                    supplierOutsideOrderHotelRoom.setId(SnowflakeIdWorker.nextId());
                    supplierOutsideOrderHotelRoom.setOrderId(supplierOrderId);
                    supplierOutsideOrderHotelRoom.setCheckInId(supplierOutsideOrderHotel.getId());
                    supplierOutsideOrderHotelRoom.setCreateId(userId);
                    supplierOutsideOrderHotelRoom.setGmtCreate(nowDateTime);
                    supplierOutsideOrderHotelRoom.setModifiedId(userId);
                    supplierOutsideOrderHotelRoom.setGmtModified(nowDateTime);
                    supplierOutsideOrderHotelRoom.setIsDeleted(IsDeletedCode.NO.getCode());
                    supplierOutsideOrderHotelRooms.add(supplierOutsideOrderHotelRoom);

                    // 新增酒店房间旅客信息
                    List<SupplierOutsideOrderHotelTouristReq> supplierOutsideOrderHotelTouristReqList = outsideOrderHotelRoomReq.getSupplierOutsideOrderHotelTouristReqList();
                    if (CollectionUtils.isNotEmpty(supplierOutsideOrderHotelTouristReqList)) {
                        supplierOutsideOrderHotelTouristReqList.forEach(outsideOrderHotelTouristReq -> {
                            SupplierOutsideOrderHotelTourist supplierOutsideOrderHotelTourist = EntityUtil.copy(outsideOrderHotelTouristReq, SupplierOutsideOrderHotelTourist.class);
                            supplierOutsideOrderHotelTourist.setId(SnowflakeIdWorker.nextId());
                            supplierOutsideOrderHotelTourist.setOrderId(supplierOrderId);
                            supplierOutsideOrderHotelTourist.setCheckInId(supplierOutsideOrderHotelRoom.getId());
                            supplierOutsideOrderHotelTourist.setCreateId(userId);
                            supplierOutsideOrderHotelTourist.setGmtCreate(nowDateTime);
                            supplierOutsideOrderHotelTourist.setModifiedId(userId);
                            supplierOutsideOrderHotelTourist.setGmtModified(nowDateTime);
                            supplierOutsideOrderHotelTourist.setIsDeleted(IsDeletedCode.NO.getCode());
                            supplierOutsideOrderHotelTourists.add(supplierOutsideOrderHotelTourist);
                        });
                    }
                });
            }
            List<SupplierOutsideOrderHotelServiceReq> supplierOutsideOrderHotelServiceReqList = outsideOrderHotelReq.getSupplierOutsideOrderHotelServiceReqList();
            if (CollectionUtils.isNotEmpty(supplierOutsideOrderHotelServiceReqList)) {
                //新增酒店附加服务详细信息
                supplierOutsideOrderHotelServiceReqList.forEach(outsideOrderHotelServiceReq -> {
                    SupplierOutsideOrderHotelService supplierOutsideOrderHotelService = EntityUtil.copy(outsideOrderHotelServiceReq, SupplierOutsideOrderHotelService.class);
                    supplierOutsideOrderHotelService.setId(SnowflakeIdWorker.nextId());
                    supplierOutsideOrderHotelService.setOrderId(supplierOrderId);
                    supplierOutsideOrderHotelService.setCheckInId(supplierOutsideOrderHotel.getId());
                    supplierOutsideOrderHotelService.setCreateId(userId);
                    supplierOutsideOrderHotelService.setGmtCreate(nowDateTime);
                    supplierOutsideOrderHotelService.setModifiedId(userId);
                    supplierOutsideOrderHotelService.setGmtModified(nowDateTime);
                    supplierOutsideOrderHotelService.setIsDeleted(IsDeletedCode.NO.getCode());
                    supplierOutsideOrderHotelServices.add(supplierOutsideOrderHotelService);

                    // 新增酒店附加服务旅客类型
                    List<SupplierOutsideOrderHotelServiceTouristTypeReq> supplierOutsideOrderHotelServiceTouristTypeReqList = outsideOrderHotelServiceReq.getSupplierOutsideOrderHotelServiceTouristTypeReqList();
                    if (CollectionUtils.isNotEmpty(supplierOutsideOrderHotelServiceTouristTypeReqList)) {
                        supplierOutsideOrderHotelServiceTouristTypeReqList.forEach(outsideOrderHotelServiceTouristTypeReq -> {
                            SupplierOutsideOrderHotelServiceTouristType supplierOutsideOrderHotelServiceTouristType = EntityUtil.copy(outsideOrderHotelServiceTouristTypeReq, SupplierOutsideOrderHotelServiceTouristType.class);
                            supplierOutsideOrderHotelServiceTouristType.setId(SnowflakeIdWorker.nextId());
                            supplierOutsideOrderHotelServiceTouristType.setOrderId(supplierOrderId);
                            supplierOutsideOrderHotelServiceTouristType.setCheckInId(supplierOutsideOrderHotelService.getId());
                            supplierOutsideOrderHotelServiceTouristType.setServiceId(outsideOrderHotelServiceReq.getId());
                            supplierOutsideOrderHotelServiceTouristType.setCreateId(userId);
                            supplierOutsideOrderHotelServiceTouristType.setGmtCreate(nowDateTime);
                            supplierOutsideOrderHotelServiceTouristType.setModifiedId(userId);
                            supplierOutsideOrderHotelServiceTouristType.setGmtModified(nowDateTime);
                            supplierOutsideOrderHotelServiceTouristType.setIsDeleted(IsDeletedCode.NO.getCode());
                            supplierOutsideOrderHotelServiceTouristTypes.add(supplierOutsideOrderHotelServiceTouristType);
                        });
                        // 新增酒店附加服务旅客
                        List<SupplierOutsideOrderHotelServiceTouristReq> supplierOutsideOrderHotelServiceTouristReqList = outsideOrderHotelServiceReq.getSupplierOutsideOrderHotelServiceTouristReqList();
                        if (CollectionUtils.isNotEmpty(supplierOutsideOrderHotelServiceTouristReqList)) {
                            supplierOutsideOrderHotelServiceTouristReqList.forEach(outsideOrderHotelServiceTouristReq -> {
                                SupplierOutsideOrderHotelServiceTourist supplierOutsideOrderHotelServiceTourist = EntityUtil.copy(outsideOrderHotelServiceTouristReq, SupplierOutsideOrderHotelServiceTourist.class);
                                supplierOutsideOrderHotelServiceTourist.setId(SnowflakeIdWorker.nextId());
                                supplierOutsideOrderHotelServiceTourist.setOrderId(supplierOrderId);
                                supplierOutsideOrderHotelServiceTourist.setCheckInId(supplierOutsideOrderHotelService.getId());
//                                supplierOutsideOrderHotelServiceTourist.setTouristTypeId(outsideOrderHotelServiceTouristTypeReq.getTouristTypeId());
                                supplierOutsideOrderHotelServiceTourist.setCreateId(userId);
                                supplierOutsideOrderHotelServiceTourist.setGmtCreate(nowDateTime);
                                supplierOutsideOrderHotelServiceTourist.setModifiedId(userId);
                                supplierOutsideOrderHotelServiceTourist.setGmtModified(nowDateTime);
                                supplierOutsideOrderHotelServiceTourist.setIsDeleted(IsDeletedCode.NO.getCode());
                                supplierOutsideOrderHotelServiceTourists.add(supplierOutsideOrderHotelServiceTourist);
                            });
                        }
                    }
                });
            }
        });
        supplierOutsideOrderHotelCheckInMapper.insertBatchSomeColumn(supplierOutsideOrderHotelCheckIns);
        supplierOutsideOrderHotelRoomMapper.insertBatchSomeColumn(supplierOutsideOrderHotelRooms);
        supplierOutsideOrderHotelTouristMapper.insertBatchSomeColumn(supplierOutsideOrderHotelTourists);
        supplierOutsideOrderHotelServiceMapper.insertBatchSomeColumn(supplierOutsideOrderHotelServices);
        supplierOutsideOrderHotelServiceTouristTypeMapper.insertBatchSomeColumn(supplierOutsideOrderHotelServiceTouristTypes);
        supplierOutsideOrderHotelServiceTouristMapper.insertBatchSomeColumn(supplierOutsideOrderHotelServiceTourists);
    }

    @Override
    public List<SupplierOutsideOrderHotelCheckInDTO> selectByOrderId(Long id) {
        List<SupplierOutsideOrderHotelCheckInDTO> supplierOutsideOrderHotelCheckInDTOList = new ArrayList<>();
        //查询酒店入住列表
        QueryWrapper<SupplierOutsideOrderHotelCheckIn> hotelWrapper = new QueryWrapper();
        hotelWrapper.lambda().eq(SupplierOutsideOrderHotelCheckIn::getOrderId, id);
        List<SupplierOutsideOrderHotelCheckIn> supplierOutsideOrderHotels = supplierOutsideOrderHotelCheckInMapper.selectList(hotelWrapper);
        if (CollectionUtils.isNotEmpty(supplierOutsideOrderHotels)) {
            supplierOutsideOrderHotelCheckInDTOList = EntityUtil.copyList(supplierOutsideOrderHotels, SupplierOutsideOrderHotelCheckInDTO.class);
            supplierOutsideOrderHotelCheckInDTOList.forEach(hotel -> {
                //查询酒店-房间列表
                QueryWrapper<SupplierOutsideOrderHotelRoom> hotelRoomWrapper = new QueryWrapper();
                hotelRoomWrapper.lambda().eq(SupplierOutsideOrderHotelRoom::getOrderId, id);
                hotelRoomWrapper.lambda().eq(SupplierOutsideOrderHotelRoom::getCheckInId, hotel.getId());
                List<SupplierOutsideOrderHotelRoom> supplierOutsideOrderHotelRoomList = supplierOutsideOrderHotelRoomMapper.selectList(hotelRoomWrapper);
                if (CollectionUtils.isNotEmpty(supplierOutsideOrderHotelRoomList)) {
                    List<SupplierOutsideOrderHotelRoomDTO> supplierOutsideOrderHotelRoomDTOList = EntityUtil.copyList(supplierOutsideOrderHotelRoomList, SupplierOutsideOrderHotelRoomDTO.class);
                    hotel.setSupplierOutsideOrderHotelRoomDTOList(supplierOutsideOrderHotelRoomDTOList);
                    supplierOutsideOrderHotelRoomDTOList.forEach(hotelRoomDTO -> {
                        //查询酒店-房间旅客列表
                        QueryWrapper<SupplierOutsideOrderHotelTourist> hotelTouristWrapper = new QueryWrapper();
                        hotelTouristWrapper.lambda().eq(SupplierOutsideOrderHotelTourist::getOrderId, id);
                        hotelTouristWrapper.lambda().eq(SupplierOutsideOrderHotelTourist::getCheckInId, hotel.getId());
                        List<SupplierOutsideOrderHotelTourist> supplierOutsideOrderHotelTouristList = supplierOutsideOrderHotelTouristMapper.selectList(hotelTouristWrapper);
                        List<SupplierOutsideOrderHotelTouristDTO> supplierOutsideOrderHotelTouristDTOList = EntityUtil.copyList(supplierOutsideOrderHotelTouristList, SupplierOutsideOrderHotelTouristDTO.class);
                        hotelRoomDTO.setSupplierOutsideOrderHotelTouristDTOList(supplierOutsideOrderHotelTouristDTOList);
                    });
                }
                //查询酒店-附加服务列表
                //查询酒店-房间列表
                QueryWrapper<SupplierOutsideOrderHotelService> hotelServiceWrapper = new QueryWrapper();
                hotelServiceWrapper.lambda().eq(SupplierOutsideOrderHotelService::getOrderId, id);
                hotelServiceWrapper.lambda().eq(SupplierOutsideOrderHotelService::getCheckInId, hotel.getId());
                List<SupplierOutsideOrderHotelService> supplierOutsideOrderHotelServiceList = supplierOutsideOrderHotelServiceMapper.selectList(hotelServiceWrapper);
                if (CollectionUtils.isNotEmpty(supplierOutsideOrderHotelServiceList)) {
                    List<SupplierOutsideOrderHotelServiceDTO> supplierOutsideOrderHotelServiceDTOList = EntityUtil.copyList(supplierOutsideOrderHotelServiceList, SupplierOutsideOrderHotelServiceDTO.class);
                    hotel.setSupplierOutsideOrderHotelServiceDTOList(supplierOutsideOrderHotelServiceDTOList);
                    supplierOutsideOrderHotelServiceDTOList.forEach(hotelServiceDTO -> {
                        //查询酒店-房间旅客类型列表
                        QueryWrapper<SupplierOutsideOrderHotelServiceTouristType> hotelServiceTouristTypeWrapper = new QueryWrapper();
                        hotelServiceTouristTypeWrapper.lambda().eq(SupplierOutsideOrderHotelServiceTouristType::getOrderId, id);
                        hotelServiceTouristTypeWrapper.lambda().eq(SupplierOutsideOrderHotelServiceTouristType::getCheckInId, hotel.getId());
                        List<SupplierOutsideOrderHotelServiceTouristType> supplierOutsideOrderHotelServiceTouristTypeList = supplierOutsideOrderHotelServiceTouristTypeMapper.selectList(hotelServiceTouristTypeWrapper);
                        if (CollectionUtils.isNotEmpty(supplierOutsideOrderHotelServiceTouristTypeList)) {
                            List<SupplierOutsideOrderHotelServiceTouristTypeDTO> supplierOutsideOrderHotelServiceTouristTypeDTOList = EntityUtil.copyList(supplierOutsideOrderHotelServiceTouristTypeList, SupplierOutsideOrderHotelServiceTouristTypeDTO.class);
                            hotelServiceDTO.setSupplierOutsideOrderHotelServiceTouristTypeDTOList(supplierOutsideOrderHotelServiceTouristTypeDTOList);
                        }
                        //查询酒店-房间旅客列表
                        QueryWrapper<SupplierOutsideOrderHotelServiceTourist> hotelServiceTouristWrapper = new QueryWrapper();
                        hotelServiceTouristWrapper.lambda().eq(SupplierOutsideOrderHotelServiceTourist::getOrderId, id);
                        hotelServiceTouristWrapper.lambda().eq(SupplierOutsideOrderHotelServiceTourist::getCheckInId, hotel.getId());
                        List<SupplierOutsideOrderHotelServiceTourist> supplierOutsideOrderHotelServiceTouristList = supplierOutsideOrderHotelServiceTouristMapper.selectList(hotelServiceTouristWrapper);
                        if (CollectionUtils.isNotEmpty(supplierOutsideOrderHotelServiceTouristList)) {
                            List<SupplierOutsideOrderHotelServiceTouristDTO> supplierOutsideOrderHotelServiceTouristDTOList = EntityUtil.copyList(supplierOutsideOrderHotelServiceTouristList, SupplierOutsideOrderHotelServiceTouristDTO.class);
                            hotelServiceDTO.setSupplierOutsideOrderHotelServiceTouristDTOList(supplierOutsideOrderHotelServiceTouristDTOList);
                        }
                    });
                }
            });
        }
        return supplierOutsideOrderHotelCheckInDTOList;
    }
}
